package com.sso.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author xiaoer
 * @date 2020/11/19 9:11
 */
@Controller
@RequestMapping("/view")
public class ViewController {

    @Autowired
    private RestTemplate restTemplate;

    private final String LOGIN_USER_ADDRESS = "http://localhost:8001/login/info?token=";

    @GetMapping("")
    public String toCart(@CookieValue(required = false,value = "TOKEN") Cookie cookie, HttpSession session){
        if(cookie!=null){
            String token = cookie.getValue();
            if(!StringUtils.isEmpty(token)){
                Map loginUser = restTemplate.getForObject(LOGIN_USER_ADDRESS+token, Map.class);
                session.setAttribute("loginUser",loginUser);
            }
        }
        return "cart";
    }
}
